package com.shenmazong.appdemodocx.utils;

import java.io.*;

public class FileUtils {

    public final static String DOCX_SUFFIX=".docx";
    public final static String ZB_FILE_SUFFIX="_zb_file.docx";
    public final static String ZB_PDF_FILE_SUFFIX="_zb_file.pdf";
    public final static String ZB_ZIP_FILE_SUFFIX="_zb_file.zip";

    public static String getFileContent(File file) throws Exception {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("读取文件异常");
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }

        return sbf.toString();
    }


    /**
     * 读取json文件，返回json串
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void updateDocumentFile(File documentFile,File needsFile) {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        boolean canAdd=false;
        try {
            reader = new BufferedReader(new FileReader(documentFile));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                if(!tempStr.contains("altChunk")){
                    sbf.append(tempStr);
                }
                if(tempStr.contains("altChunk")){
                    canAdd=true;
                }
                if(canAdd&&tempStr.contains("</w:p>")){
                    canAdd=false;
                    //插入采购需求的xml
                    String needsContent = getFileContent(needsFile);
                    if(needsContent.contains("<w:body>")&&needsContent.contains("</w:body>")){
                        String addContent=needsContent.split("<w:body>")[1].split("</w:body>")[0];
                        sbf.append(addContent);
                    }
                }
            }
            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        writeFile(documentFile.getAbsolutePath(),sbf.toString());
    }

    public static void writeFile(String filePath, String content) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            bw.write(content);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    bw = null;
                }
            }
        }
    }

    public static void delFile(File file) {
        if(!file.exists()){
            return;
        }
        if (file.isFile()) {
            file.delete();
        } else {
            File[] filenames = file.listFiles();
            for (File f : filenames) {
                delFile(f);
            }
            file.delete();
        }
    }

    public static byte[]  inputStremToBytes(InputStream is) throws IOException{
        //将输入流 转换为字节数组
        byte[] buffer=new byte[1024];
        int len=0;
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        while((len=is.read(buffer))!=-1){
            bos.write(buffer,0,len);
        }
        bos.flush();
        return bos.toByteArray();
    }


    public static void updateDocumentFileByBookMark(File documentFile,File file,String bookMark) {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        boolean canAdd2=false;
        boolean canAdd=false;
        try {
            reader = new BufferedReader(new FileReader(documentFile));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                if(!tempStr.contains("altChunk")){
                    sbf.append(tempStr);
                }
                if(tempStr.contains(bookMark)){
                    canAdd2=true;
                }
                if(tempStr.contains("altChunk")){
                    canAdd=true;
                }
                if(canAdd&&canAdd2&&tempStr.contains("</w:p>")){
                    canAdd=false;
                    //插入采购需求的xml
                    String needsContent = getFileContent(file);
                    if(needsContent.contains("<w:body>")&&needsContent.contains("</w:body>")){
                        String addContent=needsContent.split("<w:body>")[1].split("</w:body>")[0];
                        sbf.append(addContent);
                    }
                }
            }
            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        writeFile(documentFile.getAbsolutePath(),sbf.toString());
    }

    public static void updateDocumentFileByAltChunk(File documentFile,File file) {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        boolean canAdd=false;
        try {
            reader = new BufferedReader(new FileReader(documentFile));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                if(!tempStr.contains("altChunk")){
                    sbf.append(tempStr);
                }
                if(tempStr.contains("altChunk")){
                    canAdd=true;
                }
                if(canAdd&&tempStr.contains("</w:sectPr>")){
                    canAdd=false;
                    //插入采购需求的xml
                    String needsContent = getFileContent(file);
                    if(needsContent.contains("<w:body>")&&needsContent.contains("</w:body>")){
                        String addContent=needsContent.split("<w:body>")[1].split("</w:body>")[0];
                        sbf.append(addContent);
                    }
                }
            }
            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        writeFile(documentFile.getAbsolutePath(),sbf.toString());
    }


    public static void updateDocumentFileByAltChunk2(File documentFile,File file) {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        boolean canAdd=false;
        try {
            reader = new BufferedReader(new FileReader(documentFile));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                if(!tempStr.contains("altChunk")){
                    sbf.append(tempStr);
                }
                if(tempStr.contains("altChunk")){
                    canAdd=true;
                }
                if(canAdd){
                    canAdd=false;
                    //插入采购需求的xml
                    String needsContent = getFileContent(file);
                    if(needsContent.contains("<w:body>")&&needsContent.contains("</w:body>")){
                        String addContent=needsContent.split("<w:body>")[1].split("</w:body>")[0];
                        sbf.append(addContent);
                    }
                }
            }
            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        writeFile(documentFile.getAbsolutePath(),sbf.toString());
    }

}
